package com.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.R;
import com.app.Application;
import com.app.cache.SecondLevelCache;
import com.app.constant.Constants;
import com.app.constant.KeyValue;
import com.app.http.HttpDataApi;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.request.LoginReq;
import com.app.model.response.LoginRes;
import com.app.model.response.ResObj;
import com.app.utils.StringUtil;
import com.app.utils.ToastUtil;
import com.app.view.ClearWriteEditText;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ActivityLogin extends AppCompatActivity implements HttpDataApi.OnRequestCallback {

    private static final String LOG_TAG = ActivityLogin.class.getSimpleName();

    @BindView(R.id.etPhone)
    ClearWriteEditText etPhone;
    @BindView(R.id.etPwd)
    ClearWriteEditText etPwd;
    @BindView(R.id.tvRegister)
    TextView tvRegister;
    @BindView(R.id.tvForgetPwd)
    TextView tvForgetPwd;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;
    @BindView(R.id.content)
    LinearLayout content;

    private HttpDataLoader dataLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            getWindow().setStatusBarColor(getResources().getColor(R.color.light_black));
//        }
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
//        content.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        Object name = SecondLevelCache.sharedInstance().get(KeyValue.LOGIN_NAME);
        if (name != null) {
            etPhone.setText(name.toString());
        }
//        Object pwd = SecondLevelCache.sharedInstance().get(KeyValue.LOGIN_PWD);
//        if (pwd != null) {
//            etPwd.setText(pwd.toString());
//        }
        dataLoader = new HttpDataLoader(this);
    }

    private void login() {
        if (StringUtil.isEmpty(etPhone.getText().toString())) {
            ToastUtil.getInstance().showToast(ActivityLogin.this, "请输入手机号");
            return;
        }
        if (StringUtil.isEmpty(etPwd.getText().toString())) {
            ToastUtil.getInstance().showToast(ActivityLogin.this, "请输入密码");
            return;
        }
        LoginReq req = new LoginReq();
        req.setPhone(etPhone.getText().toString());
        req.setPassword(etPwd.getText().toString());
        req.setNeedAuth(false);
        dataLoader.POST(HttpDataReqUrl.URL_LOGIN, req);
        Map<String, String> map_value = new HashMap<String, String>();
        map_value.put("账号", etPhone.getText().toString());
        map_value.put("密码", etPwd.getText().toString());
        MobclickAgent.onEventValue(Application.instance, "login", map_value, 0);


    }

    @OnClick({R.id.tvRegister, R.id.tvForgetPwd, R.id.tvSubmit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvRegister:
                Intent intentRegist = new Intent(ActivityLogin.this, ActivityRegister.class);
                startActivity(intentRegist);
                finish();
                break;
            case R.id.tvForgetPwd:
                Intent intentForgetPwd = new Intent(ActivityLogin.this, ActivityForgetPwd.class);
                startActivity(intentForgetPwd);
                finish();
                break;
            case R.id.tvSubmit:
                login();
                break;
            default:
                break;
        }
    }

    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        Log.e(LOG_TAG, "onRequestSuccess");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "result: " + result);
        if (url.equals(HttpDataReqUrl.URL_LOGIN)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {
                if (result.getData() instanceof LoginRes) {
                    LoginRes loginRes = (LoginRes) (result.getData());
                    SecondLevelCache.sharedInstance().put(KeyValue.TOKEN, loginRes.getToken());
                    SecondLevelCache.sharedInstance().put(KeyValue.LOGIN_NAME, etPhone.getText().toString());
                    SecondLevelCache.sharedInstance().put(KeyValue.LOGIN_PWD, etPwd.getText().toString());
                    Map<String, String> map_value = new HashMap<String, String>();
                    map_value.put("账号", etPhone.getText().toString());
                    map_value.put("密码", etPwd.getText().toString());
                    MobclickAgent.onEventValue(Application.instance, "loginSuccess", map_value, 0);
                    MobclickAgent.onProfileSignIn(etPhone.getText().toString());
                    // 登录成功，关闭页面
                    finish();
                    overridePendingTransition(0, R.anim.push_left_out);
                } else {
                    ToastUtil.getInstance().showToast(this, result.getMsg());
                }
            } else {
                Map<String, String> map_value = new HashMap<String, String>();
                map_value.put("账号", etPhone.getText().toString());
                map_value.put("密码", etPwd.getText().toString());
                MobclickAgent.onEventValue(Application.instance, "loginFail", map_value, 0);
                ToastUtil.getInstance().showToast(this, result.getMsg());
            }
        }
    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        Log.e(LOG_TAG, "onRequestError");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "statusCode: " + statusCode);
        ToastUtil.getInstance().showToast(this, error);
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.push_left_out);
    }
}
